package cn.iocoder.yudao.module.mqtt.controller.admin.scmc;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mqtt.controller.admin.scmc.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.scmc.ScmcDO;
import cn.iocoder.yudao.module.mqtt.service.scmc.ScmcService;

@Tag(name = "管理后台 - 监测局")
@RestController
@RequestMapping("/mqtt/scmc")
@Validated
public class ScmcController {

    @Resource
    private ScmcService scmcService;

    @PostMapping("/create")
    @Operation(summary = "创建监测局")
    @PreAuthorize("@ss.hasPermission('mqtt:scmc:create')")
    public CommonResult<Integer> createScmc(@Valid @RequestBody ScmcSaveReqVO createReqVO) {
        return success(scmcService.createScmc(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新监测局")
    @PreAuthorize("@ss.hasPermission('mqtt:scmc:update')")
    public CommonResult<Boolean> updateScmc(@Valid @RequestBody ScmcSaveReqVO updateReqVO) {
        scmcService.updateScmc(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除监测局")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mqtt:scmc:delete')")
    public CommonResult<Boolean> deleteScmc(@RequestParam("id") Integer id) {
        scmcService.deleteScmc(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除监测局")
                @PreAuthorize("@ss.hasPermission('mqtt:scmc:delete')")
    public CommonResult<Boolean> deleteScmcList(@RequestParam("ids") List<Integer> ids) {
        scmcService.deleteScmcListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得监测局")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mqtt:scmc:query')")
    public CommonResult<ScmcRespVO> getScmc(@RequestParam("id") Integer id) {
        ScmcDO scmc = scmcService.getScmc(id);
        return success(BeanUtils.toBean(scmc, ScmcRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得监测局分页")
    @PreAuthorize("@ss.hasPermission('mqtt:scmc:query')")
    public CommonResult<PageResult<ScmcRespVO>> getScmcPage(@Valid ScmcPageReqVO pageReqVO) {
        PageResult<ScmcDO> pageResult = scmcService.getScmcPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ScmcRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出监测局 Excel")
    @PreAuthorize("@ss.hasPermission('mqtt:scmc:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportScmcExcel(@Valid ScmcPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ScmcDO> list = scmcService.getScmcPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "监测局.xls", "数据", ScmcRespVO.class,
                        BeanUtils.toBean(list, ScmcRespVO.class));
    }

}